package com.yw.smartcity_study.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.yw.smartcity_study.App;
import com.yw.smartcity_study.HomeBannerRes;
import com.yw.smartcity_study.R;
import com.yw.smartcity_study.ServiceCreator;
import com.yw.smartcity_study.ServicesAdapter;
import com.yw.smartcity_study.api.AllServiceRes;
import com.yw.smartcity_study.api.ICallback;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeFragment extends Fragment {

    //fragment是一个片段  它不能独立运行，必须依赖在activity上运行
    //activity可以动态的添加fragment  移除

    private Banner bannerHome;
    private GridView gridview;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 将布局文件转成view
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        bannerHome = (Banner) view.findViewById(R.id.banner_home);
        bannerHome.setIndicator(new CircleIndicator(getContext()));

        gridview = (GridView) view.findViewById(R.id.gridview);


        getBannerList();
        getAllServiceList();

        return view;
    }

    private void getAllServiceList() {
        ServiceCreator.getInstance().getApi().getServiceList().enqueue(new ICallback<AllServiceRes>() {
            @Override
            public void onSuccess(AllServiceRes response) {
                //
                List<AllServiceRes.RowsBean> datas =  response.getRows();
                //只接收前7个数据
                AllServiceRes.RowsBean more = new AllServiceRes.RowsBean();
                more.setServiceName("更多服务");

                List<AllServiceRes.RowsBean> newDatas = new ArrayList<>();
                for(int i=0;i<datas.size();i++){
                    if(i<7){
                        newDatas.add(datas.get(i));
                    }else{
                        break;
                    }
                }
                newDatas.add(more);

                ServicesAdapter servicesAdapter = new ServicesAdapter();
                servicesAdapter.setDatas(newDatas);
                gridview.setAdapter(servicesAdapter);
            }
        });


    }

    private void getBannerList(){
        ServiceCreator.getInstance().getApi().getBannerList().enqueue(new ICallback<HomeBannerRes>() {
            @Override
            public void onSuccess(HomeBannerRes response) {
                //拿到了轮播图的数据  然后渲染
                bannerHome.setAdapter(new BannerImageAdapter<HomeBannerRes.RowsBean>(response.getRows()) {


                    @Override
                    public void onBindView(BannerImageHolder bannerImageHolder, HomeBannerRes.RowsBean rowsBean, int i, int i1) {
                        //bannerImageHolder.imageView.setImageResource();
                        //加载网络图片
                        Glide.with(getContext()).load(App.baseUrl+rowsBean.getAdvImg()).into(bannerImageHolder.imageView);
                    }
                });
            }
        });
    }
}